<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/21 0021
 * Time: 上午 10:13
 */
namespace app\admin\controller;
use app\common\controller\Baseadmin;
use app\common\model\Adminuser;
use think\App;
use think\facade\Config;
use think\facade\Session;
class  Index extends Baseadmin {
     protected $res;
    //依赖注入
    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->res=$this->islogin();
    }
    /**
     * @return string
     * @throws \Exception
     * 首页面
     */
    public function index(){
        if($this->res==true){
           return $this->view->fetch();
        }else{
           $this->redirect('login');
        }
    }

    /**
     * @return string
     * @throws \Exception
     * 登录页面
     */
    public function login(){
           if($this->request->isAjax()){
               $requestdata=$this->request->param();
               if(!$requestdata['name'] or !$requestdata['password']){return json(['msg'=>'账号和密码不能为空','code'=>0]);}
               $salt=Config::get('salt');
               $requestdata['password'] = sha1($salt.$requestdata['password']);
               $res=Adminuser::get($requestdata);
               if($res && $res['status']==1 ){
                   Session::set('userid',$res['id']);
                   Session::set('groupid',$res['group_id']);
                   Session::set('name',$res['name']);
                   Session::set('userinfo',$res);
                   $this->adduserlog();
                   $this->success('登录成功','Index/index');
               }elseif($res && $res['status']!=1){
                   return json(['msg'=>'此账号被拉黑，请联系管理员','code'=>0]);
               }else{
                   return json(['msg'=>'账号或密码错误，请重新登录！','code'=>0]);
               }
           }else{
               return $this->view->fetch();
           }
    }
    public function welcome(){
        return view('');
    }
}